import 'dart:convert';

import 'package:liuxlcommon/util/string_utils.dart';
import 'package:ladder_cash/app/com/manage/l_state_manage/l_get_controller.dart';
import 'package:ladder_cash/app/key_mix.dart';
import 'package:ladder_cash/app/page/product_detail/authentication_page.dart';
import 'package:ladder_cash/generated/l10n.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/util/toast_util.dart';
import 'package:ladder_cash/app/com/http/http_request.dart';
import 'package:ladder_cash/app/com/service/burial_point_service.dart';
import 'package:ladder_cash/app/com/view/webview/my_web/my_web_page.dart';

import '../../../library/src/logger.dart';

class ProductDetailLogic extends LGetController {
  late String product_id;
  bool agree = true;

  ///详情页面所有数据
  Map? detailData;

  ///认证项列表
  List verifyList = [];

  bool isCompleteAll = false;

  preInit({arguments}) async {
    try {
      product_id = arguments['product_id'.hx];
      var detailData = await getDetailData(loadingText: S.current.loading);
      setData(detailData);
    } catch (e) {
      // Get.back();
    }
  }

  onInit() {
    try {
      product_id = Get.arguments['product_id'];
      setData(Get.arguments['detailData']);
    } catch (e) {
      // Get.back();
    }
  }

  ///获取产品详情
  getDetailData({refresh = false, loadingText}) async {
    var detailData = await HttpRequest().post(
        '/v3/product/detail'.hx, {'product_id'.hx: product_id},
        loadingText: loadingText);
    setData(detailData);
    if (refresh) {
      update();
    }
    return detailData;
  }

  setData(detailData) {
    this.detailData = detailData;
    verifyList = detailData?['verify'.hx];
    if (detailData != null) {
      String next = detailData?['nextStep'.hx]?['taskType'.hx] ?? "";
      if (next.isEmpty) {
        //认证完成
        isCompleteAll = true;
      }
    }
  }

  next({item}) async {
    bool isBack = false;
    Map detailData2 = json.decode(json.encode(detailData));

    if (StringUtils.isEmpty(detailData2['nextStep'.hx]?['taskType'.hx])) {
      detailData2['nextStep'.hx] = {};
      isBack = true;
    } else {
      if (item['status'.hx] == 0) {
        item['taskType'.hx] = detailData2['nextStep'.hx]?['taskType'.hx];
        item['title'.hx] = detailData2['nextStep'.hx]?['title'.hx];
      }
    }
    detailData2['nextStep'.hx]?['taskType'.hx] = item['taskType'.hx];
    detailData2['nextStep'.hx]?['title'.hx] = item['title'.hx];
    Get.to(AuthenticationPage(), arguments: {
      'productData': detailData2,
      'product_id': product_id,
      'isBack': isBack
    });
  }

  burialPoint() async {
    await BurialPointService.start(BPoint.startApply,
        ache: detailData?['productDetail'.hx]?['orderNo'.hx]?.toString() ?? '',
        waitLocation: true);
    BurialPointService.end(BPoint.startApply,
        ache: product_id,
        orderNo:
            detailData?['productDetail'.hx]?['orderNo'.hx]?.toString() ?? '');
  }

  borrowMoney() async {
    Logger.d("borrowMoney");
    burialPoint();
    dynamic data = await HttpRequest().post(
        '/v3/product/push'.hx,
        <String, dynamic>{
          'order_no'.hx:
              detailData?['productDetail'.hx]['orderNo'.hx]?.toString() ?? '',
          'amount'.hx:
              detailData?['productDetail'.hx]['amount'.hx]?.toString() ?? '',
          'term'.hx:
              detailData?['productDetail'.hx]['term'.hx]?.toString() ?? '',
          'term_type'.hx:
              detailData?['productDetail'.hx]['term_type'.hx]?.toString() ?? ''
        },
        loadingText: S.current.loading);
    if (data != null) {
      Get.off(MyWebPage(url: data['url'.hx]));
    }
  }
}
